package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.wanmait.spacesys.dao.CommentDAO;
import com.wanmait.spacesys.dao.PostDAO;
import com.wanmait.spacesys.vo.Comment;
import com.wanmait.spacesys.vo.Post;
import com.wanmait.spacesys.vo.Userinfo;

@WebServlet("/PostController")
@MultipartConfig
public class PostController extends BaseController{
	PostDAO postDAO = new PostDAO();
	CommentDAO commentDAO = new CommentDAO();
	//postajax.jsp的列表
	
	protected void search(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String text = request.getParameter("s");
		List<Post> texts = postDAO.search(text);
		request.setAttribute("texts", texts);
		this.forward("/web/searchResult.jsp", request, response);
		
	}
	//ajax显示文章列表
	protected void ajaxlist(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String pageNumStr = request.getParameter("pageNum");
		int pageNum = 1;
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		List<Post> hotPosts = postDAO.hotFindAll(pageNum);
		request.setAttribute("hotPosts", hotPosts);
		List<Post> newPosts = postDAO.newFindAll(pageNum);
		request.setAttribute("newPosts", newPosts);
		this.forward("/web/postajax.jsp", request, response);
	}
	//bbsindex.jsp里的文章列表
	protected void list(HttpServletRequest request, HttpServletResponse response) //bbsindex.jsp的文章列表
			throws ServletException, IOException {
		String pageNumStr = request.getParameter("pageNum");
		int pageNum = 1;
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		int count = postDAO.getRecordCount();  //分页
		request.setAttribute("count", count);
		List<Post> hotPosts = postDAO.hotFindAll(pageNum); //最热文章列表
		request.setAttribute("hotPosts", hotPosts);
		List<Post> newPosts = postDAO.newFindAll(pageNum); //最新文章列表
		request.setAttribute("newPosts", newPosts);
		this.forward("/web/bbsindex.jsp", request, response);
	}
	//single.jsp的评论列表
	protected void commentAjaxList(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		List<Comment> comments = commentDAO.findAll(id);
		request.setAttribute("comments", comments);
		this.forward("/web/commentajax.jsp", request, response);
	}
	//single.jsp下级评论
	protected void downCommentAjax(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		String idStr = request.getParameter("id");			//帖子Id
		Integer id = Integer.parseInt(idStr);
		List<Comment> downComments = commentDAO.findByPid(id);
		JSONArray.writeJSONString(response.getWriter(),downComments);
	}
	//举报
	protected void report(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");  //当前登录用户
		if(userinfo==null) {
			this.redirect("/web/userLogin.jsp", request, response);
		}
		String idStr = request.getParameter("commentId");
		System.out.println(idStr);
		Integer id = Integer.parseInt(idStr);     //被举报的评论id
		String because = request.getParameter("because");		//举报的原因
		int beReportId = commentDAO.beReportUser(id);			//被举报人的Id
		commentDAO.report(id, userinfo.getId(), because,beReportId);
//		request.setAttribute("report", report);
		this.forward("/web/reportAjax.jsp", request, response);
	}
	//回复评论
	protected void reple(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			this.redirect("/web/userLogin.jsp", request, response);
		}
		String pidStr = request.getParameter("commentPid");
		Integer pid = Integer.parseInt(pidStr);
		String repleContent = request.getParameter("repleContent");
		String idStr = request.getParameter("postId");
		Integer id = Integer.parseInt(idStr);
		commentDAO.reple(id, repleContent, userinfo, pid);
		this.forward("/web/repleAjax.jsp", request, response);
		
	}
	//帖子详情页和随机取六篇文章
	protected void postSingle(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		Post post = postDAO.findById(id);
		request.setAttribute("post", post);
		List<Post> randomPosts = postDAO.randomList();
		request.setAttribute("randomPosts", randomPosts);
		this.forward("/web/single.jsp", request, response);
	}
	//发布文章
	protected void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			this.redirect("/web/userLogin.jsp", request, response);
		}
		Post post = new Post();
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		post.setTitle(title);
		post.setContent(content);
		post.setUserinfo(userinfo);
		postDAO.insert(post);
		this.forward("/PostController?action=list", request, response);

	}
	//判断禁言
	protected void spack(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		int count = commentDAO.spack(userinfo.getId());
		response.getWriter().write(count+"");
	}
	//发表评论
	protected void addComment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			this.redirect("/web/userLogin.jsp", request, response);
		}
		String idStr = request.getParameter("id");   
		Integer id = Integer.parseInt(idStr);	//帖子的Id
		Comment comment = new Comment();
		String content = request.getParameter("content");  //评论的内容
		comment.setPostId(id);
		comment.setContent(content);
		comment.setUserinfo(userinfo);
		commentDAO.insert(comment);
		this.forward("/PostController?action=postSingle", request, response);

	}
	//点赞
	protected void praised(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo guestId = (Userinfo)request.getSession().getAttribute("userinfo"); //guestId 点赞的用户Id
		String idStr =  request.getParameter("id");
		Integer id = Integer.parseInt(idStr);	//帖子Id
		int userId = postDAO.praisedUserinfo(id); //userId 被点赞的人Id	
		if(guestId.getId()==0&&userId==0&&guestId.getId()==userId) {
			return;
		}
		else {
			postDAO.praised(id,guestId.getId(),userId);
		}
	}
	//取消点赞
	protected void cancelPraised(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Userinfo guestId = (Userinfo)request.getSession().getAttribute("userinfo"); //guestId 点赞的用户Id
		String idStr =  request.getParameter("id");
		Integer id = Integer.parseInt(idStr);	//帖子Id
		int userId = postDAO.praisedUserinfo(id); //根据帖子Id查找 被点赞的人Id userId	
		int likeId = postDAO.cancelLikeId(id,guestId.getId(),userId);  //得到点赞表里的组件Id，用来删除（取消点赞）
		postDAO.cancelLike(likeId,id,userId);
	}
	//通过显示      
			protected void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				request.setCharacterEncoding("UTF-8");
				String pageNums = request.getParameter("pageNum");
				int pageNum=1; //定义变量  
				try {
					pageNum= Integer.parseInt(pageNums);//强制转化
				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				
				Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");//取数据
				if(userinfo!=null) {
					int count = postDAO.getCount(userinfo.getId());//查出分页的总数
					List<Post> posts = postDAO.adopt(userinfo.getId(),pageNum);//显示分页
					request.setAttribute("posts",posts);			  
					request.setAttribute("count", count);
					request.getRequestDispatcher("/web/user/notThrough.jsp").forward(request, response);
				}
				
			}
		
			//protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
			//{
				//String idStr =request.getParameter("id");
				//Integer id = Integer.parseInt(idStr);
				//postDAO.delete(id);
			//}
			
			
			protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
			{
			  String idstr =request.getParameter("id");
			  Integer id = Integer.parseInt(idstr);
			  
			  postDAO.plusdelete(id);
			  
			  //this.redirect("/PostController?action=show", request, response);
			}
			
			//沒有通過=====
			protected void Fail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				request.setCharacterEncoding("UTF-8");
				String pageNums=request.getParameter("pageNum");
				int pageNum=1; //定义变量  
				try {
					pageNum= Integer.parseInt(pageNums);//强制转化
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					
				}
				Userinfo usernifo= (Userinfo) request.getSession().getAttribute("userinfo");
				
				
				if(usernifo!=null)
				{	int count=postDAO.getCount(usernifo.getId());
					List<Post> posts = postDAO.list(usernifo.getId(),pageNum);
					request.setAttribute("posts",posts);
					request.setAttribute("count", count);
					request.getRequestDispatcher("/web/user/fail.jsp").forward(request, response);
				}
						
				
			}
			
		   //修改
			
			protected void preservation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	      {	
				String title=request.getParameter("title");		
			    //String content = request.getParameter("content");
			    
			    String idStr = request.getParameter("id");//获得用户id
			    Integer id = Integer.parseInt(idStr);
			   
			   
				Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
				
				if(userinfo!=null)
				{
					Post post=new Post();
					post.setId(id);
					post.setTitle(title);
					//post.setContent(content);
					  postDAO.update(post,userinfo.getId()); 
					  this.redirect("/PostController?action=Fail", request, response);//重定向过去
				/* request.setAttribute("post", post); */
//					 request.getRequestDispatcher("/web/user/fail.jsp").forward(request, response);
				}
					
	           }
			//未通过标题帖子
			protected void adoptshow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
				String ids=request.getParameter("id");
				Integer id=Integer.parseInt(ids);
				
				if(userinfo!=null) {
					 Post post= postDAO.myfindAllByPostId(userinfo.getId(), id);
					 request.setAttribute("post", post);
					
					 this.forward("/web/user/postshow.jsp", request, response);
				}
						
			}
			
			//通过标题帖子
					protected void Failposts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
						Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
						String ids=request.getParameter("id");
						Integer id=Integer.parseInt(ids);
						//System.out.println(userinfo+"**************");
						if(userinfo!=null) {
							 Post post= postDAO.myfindAllByPostId(userinfo.getId(), id);
							 request.setAttribute("post", post);
							
							 this.forward("/web/user/throughdisplay.jsp", request, response);
						}
					}
					
					

		protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				String idStr = request.getParameter("id");
				Integer id = Integer.parseInt(idStr);

			    
				Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
				
				if(userinfo!=null)
				{
					
					Post post=postDAO.findPostId(userinfo.getId(), id);  
					post.getTitle();
					request.setAttribute("post", post);
					this.forward("/web/user/modificationfailed.jsp", request, response);
				}
					}
		
		
		
//			protected void Postsave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//			{
//				String title = request.getParameter("title");
//				String content = request.getParameter("content");
//				String idStr = request.getParameter("id");
//	   		    Integer price = Integer.parseInt(idStr);
//				Integer id = null;
//				
//				try {
//					id = Integer.parseInt(idStr);
//				} catch (NumberFormatException e) {
//			    }
	//	
//			
//			   Post post = new Post();
//			   post.setId(id);
//			   post.setTitle(title);
//			   post.setContent(content);
//			   
//			   if(post.getId()==null)
//			   
//				   postDAO.postmodify(id);
//			   
//			   else 
//				   postDAO.update(post);
//			   
//			}
//		//帖子总数量
//		protected void Postsave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//		{
//			  Userinfo usernifo= (Userinfo) request.getSession().getAttribute("userinfo");
//			  if(usernifo!=null)
//			  {
//				  int count = postDAO.getCount(usernifo.getId());
//				  
//				  request.setAttribute("count", count);
//				  this.forward("/web/user/notThrough.jsp", request, response);
//			  }
//			
//		}
	

	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

}
